package com.youan.serve.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.youan.pangu.security.bean.LoginInfor;
import com.youan.serve.controller.swagger.NoticeControllerSwagger;
import com.youan.serve.dao.domain.NoticeUser;
import com.youan.serve.dao.domain.User;
import com.youan.serve.service.INoticeService;
import com.youan.serve.dao.domain.Notice;
import com.youan.pangu.base.util.PrimaryKeyUtil;
import com.youan.serve.service.INoticeUserService;
import com.youan.serve.service.IUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto_code
 * @since 2020-03-12
 */
@Controller
@RequestMapping("/notice")
public class NoticeController implements NoticeControllerSwagger{
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private INoticeUserService noticeUserService;
    @Autowired
    private IUserService UserService;

    @Override
    @PostMapping("/notice")
    public Notice addNotice(Notice notice, LoginInfor loginInfor){
        String noticeId = PrimaryKeyUtil.getCommonId();
        notice.setNoticeId(noticeId);
        notice.setNoticeTime(new Date());
        notice.setNoticeSendUserId(loginInfor.getUserId());
        noticeService.save(notice);
        String[] orgIds = notice.getOrgIds();
        List<User> userList = UserService.list(new LambdaQueryWrapper<User>().in(User::getOrgId, Arrays.asList(orgIds)));
        for (User user : userList){
            NoticeUser noticeUser = new NoticeUser();
            noticeUser.setNoticeId(noticeId);
            noticeUser.setUserNoticeId(PrimaryKeyUtil.getCommonId());
            noticeUser.setUserId(user.getUserId());
            noticeUserService.save(noticeUser);
        }
        return notice;
    }

    @Override
    @DeleteMapping("/notice/{noticeId}")
    public void deleteNoticeByNoticeId(@PathVariable String noticeId){
        noticeService.removeById(noticeId);
    }

    @Override
    @GetMapping("/notice/{noticeId}")
    public Notice selectNoticeByNoticeId(@PathVariable String noticeId){
        return noticeService.getById(noticeId);
    }

    @Override
    @PutMapping("/notice")
    public Notice updateNotice(Notice notice){
        noticeService.updateById(notice);
        return notice;
    }


    @Override
    @GetMapping("/notices")
    public List<Notice> selectAllNoticeByUser(LoginInfor loginInfor){
        List<NoticeUser> noticeUserList = noticeUserService.list(new LambdaQueryWrapper<NoticeUser>().eq(NoticeUser::getUserId, loginInfor.getUserId()));
        List<String> noticeIds = new ArrayList<String>();
        for(NoticeUser noticeUser : noticeUserList){
            noticeIds.add(noticeUser.getNoticeId());
        }
        if(noticeIds.size()<=0){
            return null;
        }
        List<Notice> noticeList = noticeService.list(new LambdaQueryWrapper<Notice>().in(Notice::getNoticeId, noticeIds).orderBy(true, true, Notice::getNoticeTime));
        for(Notice notice : noticeList){
            for(NoticeUser noticeUser : noticeUserList){
                if(notice.getNoticeId().equals(noticeUser.getNoticeId())){
                    notice.setIsRead(noticeUser.getIsRead());
                }
            }
        }
        return noticeList;
    }

}

